package org.bikelab.se3.job;

import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableOutputFormat;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.NullOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.bikelab.se3.mr.NQuadContextIndexer;
import org.bikelab.se3.mr.NQuadHbaseLoader;
import org.bikelab.se3.mr.ContextIndexedTableWriter;
import org.bikelab.se3.rdf.reader.RdfReaderFactory;
import org.bikelab.se3.rdf.reader.TrigLineReader;
import org.semanticweb.yars.nx.Node;

public class Trig2Hbase extends Configured implements Tool {
	@Override
	public int run(String[] args) throws Exception {
        Job job = new Job(getConf(), "Trig2Hbase");
        job.setJarByClass(Trig2Hbase.class);
        job.getConfiguration().set("hbase.master","i-10-26-2-22");
        
		job.setMapperClass(NQuadContextIndexer.class);
//		job.setReducerClass(NQuadHbaseLoader.Reduce.class);
//		job.setNumReduceTasks(2);
		
		job.setInputFormatClass(TextInputFormat.class);
		FileInputFormat.setInputPaths(job, new Path(args[0]));
		
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);
		
//		job.setOutputFormatClass(TableOutputFormat.class);

        TableMapReduceUtil.initTableReducerJob("lld", ContextIndexedTableWriter.class, job);
		RdfReaderFactory.addClassToConf(job.getConfiguration(), TrigLineReader.class);
		
		boolean success = job.waitForCompletion(true);
		return success ? 0 : 1;
	}
	
//    public static void main(String[] args) throws Exception {
//        System.exit(job.waitForCompletion(true) ? 0 : 1);
//    }
    
	public static void main(String[] args) throws Exception {
		int res = ToolRunner.run(new Configuration(), new Trig2Hbase(), args);
		System.exit(res);
	}
}